{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# numpy的使用示例",
   "id": "848d64a7cee6effb"
  },
  {
   "metadata": {
    "SqlCellData": {
     "variableName$1": "df_sql1"
    }
   },
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "%%sql\n",
   "id": "141daaf492f756da"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-28T06:51:16.117415Z",
     "start_time": "2025-10-28T06:51:16.113441Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# 使用numpy创建一个1维的数组\n",
    "a = np.array([1,2,3,4,5])\n",
    "\n",
    "\n",
    "# 使用numpy创建一个2维的数组\n",
    "b = np.array([[1,2,3,4,5],[1,2,3,4,5]])\n",
    "\n",
    "# 使用numpy创建一个3维的数组\n",
    "c = np.array(\n",
    "    [[[[1,2,3,4,5],[1,2,3,4,5]],[[1,2,3,4,5],[1,2,3,4,5]],[[1,2,3,4,5],[1,2,3,4,5]]]]\n",
    ")\n",
    "c"
   ],
   "id": "c5c46dcf02703146",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[1, 2, 3, 4, 5],\n",
       "         [1, 2, 3, 4, 5]],\n",
       "\n",
       "        [[1, 2, 3, 4, 5],\n",
       "         [1, 2, 3, 4, 5]],\n",
       "\n",
       "        [[1, 2, 3, 4, 5],\n",
       "         [1, 2, 3, 4, 5]]]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-28T06:52:47.241075Z",
     "start_time": "2025-10-28T06:52:47.227440Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用numpy创建一个全部都是0的数组\n",
    "d = np.zeros((3,3))\n",
    "d\n",
    "\n"
   ],
   "id": "1c1259aa83a21d2a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-28T06:53:37.380571Z",
     "start_time": "2025-10-28T06:53:37.377871Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用numpy创建一个全部都是0的数组\n",
    "d = np.ones((3,3))\n",
    "d"
   ],
   "id": "5affc17ec55affb7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-28T06:55:00.980767Z",
     "start_time": "2025-10-28T06:55:00.977880Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用numpy创建一个单位矩阵\n",
    "d = np.eye(3)\n",
    "d"
   ],
   "id": "a8e05bfe835dc423",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-28T06:59:06.740849Z",
     "start_time": "2025-10-28T06:59:06.738342Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用numpy 创建数组， 进行加减乘除运算\n",
    "g = np.array([1,2,3,4,5])\n",
    "h = np.array([7,8,9,10,1]) # 元素个数不能不一致\n",
    "print(g+h)\n"
   ],
   "id": "623194ce518f2212",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 8 10 12 14  6]\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-28T06:59:32.949283Z",
     "start_time": "2025-10-28T06:59:32.946542Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(g*h)\n",
    "print(np.multiply(g,h))"
   ],
   "id": "227ecc79defc1468",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 7 16 27 40  5]\n",
      "[ 7 16 27 40  5]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# numpy的广播机制",
   "id": "5ca37ed8f227e9fe"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-28T07:07:31.388992Z",
     "start_time": "2025-10-28T07:07:31.385499Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用Numpy创建一个2维数组, 3*2的数组\n",
    "k = np.array([[1,2],[2,3],[3,4]])\n",
    "l = np.array([3,4])\n",
    "\n",
    "print(k*l)\n",
    "\n",
    "print(\"k的形状\", k.shape)\n",
    "print(\"l的形状\", l.shape)\n",
    "print(\"广播后的k的形状:\", np.broadcast_arrays(k,l)[0].shape)\n",
    "print(\"广播后的l的形状:\", np.broadcast_arrays(k,l)[1].shape)\n"
   ],
   "id": "3771958971ba085d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 3  8]\n",
      " [ 6 12]\n",
      " [ 9 16]]\n",
      "k的形状 (3, 2)\n",
      "l的形状 (2,)\n",
      "广播后的k的形状: (3, 2)\n",
      "广播后的l的形状: (3, 2)\n"
     ]
    }
   ],
   "execution_count": 43
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yolotorch",
   "language": "python",
   "name": "yolotorch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
